// 数组栈: 尾部作为栈顶
public class ArrayStack<E> implements Stack<E> {
    private Array<E> array;
    public ArrayStack() {
        array = new Array<>();
    }

    @Override
    public void push(E elem) {
        array.addLast(elem);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public int size() {
        return array.size();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("ArrayStack: \n");
        res.append(" > [");
        for (int i = 0; i < array.size(); i++) {
            res.append(array.get(i));
            if (i != array.size() - 1)
                res.append(", ");
        }
        res.append("] top");
        return res.toString();
    }
}
